﻿using Developer.Entity;
using Developer.Service;
using Super.Framework.Common;
using Super.Framework.Common.Web;
using Super.Framework.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Developer.Web.Areas.Admin.Controllers
{
    public class MessageController : BaseController
    {
#region 发送通知
        //发送消息
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ChooseUsers()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var contents = Query.Value("content");
            var title = Query.Value("title");
            var key = Query.Value("name");
            int pageCount;
            int itemCount;
            var users = UserService.GetAll().Where(a => a.IsAbort == false);

            if (key.IsNotNullOrEmpty())
            {
                users = users.Where(a => a.Account.Contains(key));
                ViewBag.Name = key;
            }
            var items = users.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex);

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            ViewBag.contents = contents;
            ViewBag.title = title;
            return View(items);
        }
        [HttpPost]
        public JsonResult ChooseSingleUser()
        {
          
            var uid = Query.Value("uid").ToInt();
            var title = Query.Value("title");
            var content = Query.Value("content");
            var companytype = new Messages();
            if (false)
            {
                
            }
            else
            {
                companytype.Contents = content;
                companytype.IsRead = false;
                companytype.Title = title;
                companytype.CreateTime = DateTime.Now;
                companytype.UserId = uid;
                MessageService.Add(companytype);
                return Json(new { success = "已发送" }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult ChooseBatUser()
        {
            var typeIds = Query.Value("ids[]").Split(',');
            var title = Query.Value("title");
            var content = Query.Value("content");
            if (typeIds.Length < 1) return Json(new { error = "错误"},JsonRequestBehavior.AllowGet);
            try
            {
                MessageService.BatSend(typeIds, new Messages { Title = title, Contents = content, CreateTime = DateTime.Now, IsRead = false });
                return Json(new { success = "发送成功", JsonRequestBehavior.AllowGet });
            }catch(Exception e)
            {
                return Json(new { error = e.ToString() }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult ChooseAllUser()
        {
            var allUser = UserService.GetAll().Where(a => a.IsAbort == false);
            string[] typeIds = new string[allUser.Count()];
            int i = 0;
            foreach(var item in allUser){
                typeIds[i++] = item.Id.ToString();
            }
            var title = Query.Value("title");
            var content = Query.Value("content");
            try
            {
                MessageService.BatSend(typeIds, new Messages { Title = title, Contents = content, CreateTime = DateTime.Now, IsRead = false });
                return Json(new { success = "发送成功", JsonRequestBehavior.AllowGet });
            }
            catch (Exception e)
            {
                return Json(new { error = e.ToString() }, JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        #region 用户反馈

        public ActionResult FeedBack()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;
            var users = FeedBackService.GetAll().Where(a=>a.State == 0 || a.State == 1);

            if (key.IsNotNullOrEmpty())
            {
                users = users.Where(a => a.Content.Contains(key));
                ViewBag.Name = key;
            }
            var items = users.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex);

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }

        
        public ActionResult ReturnContent()
        {
            var type = Query.Value("type").ToInt();

            var time = FeedBackService.GetAll().FirstOrDefault(a => a.Id == type);
            return View(time);
        }

        [HttpPost]
        public ActionResult ReturnContent(int? z)
        {
            var type = Query.Value("type").ToInt();
            var deadtime = Query.Value("deadtime");
            //var url = Query.Value("url");
            var time = FeedBackService.GetAll().FirstOrDefault(a => a.Id == type);
            time.ReContent = deadtime;
            FeedBackService.Change(time);
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/message/returnContent/?type=" + type);
        }
        [HttpPost]
        public JsonResult SetRead()
        {
            var type = Query.Value("fid").ToInt();
            FeedBackService.ChangeState(0, type);
            return Json(new { success = "设置成功" }, JsonRequestBehavior.AllowGet);
        }
        #endregion

    }
}